/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bankroll.poker.dao;

import br.com.bankroll.poker.bean.BankRoll;
import br.com.bankroll.poker.facotry.DaoFactory;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * Classe de persistencia no banco
 * @author Paula
 */
public class BankRollDAOImpl implements IBankRollDAO {

    /**
     * Objeto private utilizado apenas como referencia da Session
     */
    private Session session;
 
    @Override
    public BankRoll getBankRollbyId(Integer id) {
        session = DaoFactory.getSessionFactory().openSession();
        session.beginTransaction();
        BankRoll bankroll = null;
        try {
            bankroll = (BankRoll) session.get(BankRoll.class, id);
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
        } finally {
            session.close();
        }
        return bankroll;
    }
    
    @Override
    public List<BankRoll> getAllBankRoll() {
        session = DaoFactory.getSessionFactory().openSession();
        session.beginTransaction();
        List<BankRoll> lstBankroll = null;
        try {
            lstBankroll = session.createCriteria(BankRoll.class).list(); 
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
        } finally {
            session.close();
        }
        return lstBankroll;
    }
    
    @Override
    public List<BankRoll> getAllBankRollByData(Date dataInicial, Date dataFinal){
        session = DaoFactory.getSessionFactory().openSession();
        session.beginTransaction();
        List<BankRoll> lstBankroll = null;
        try {
            lstBankroll = session.createCriteria(BankRoll.class) 
            .add( Restrictions.between("data", dataInicial, dataFinal) )
            .addOrder( Order.desc("data") )
            .list();
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
        } finally {
            session.close();
        }
        return lstBankroll;
    }

    @Override
    public Boolean insertBankRoll(BankRoll bankroll) {
        session = DaoFactory.getSessionFactory().openSession();
        session.beginTransaction();

        try {
            session.persist(bankroll);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
            return false;
        } finally {
            session.close();
        }
    }
    
    @Override
    public Boolean updateBankRoll(BankRoll bankroll) {
        session = DaoFactory.getSessionFactory().openSession();
        session.beginTransaction();

        try {
            session.merge(bankroll);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
            return false;
        } finally {
            session.close();
        }
    }
}
